package mac5789.projeto4.model;

import java.util.HashMap;
import java.util.Map;

/**
 * Implementação de RewardModel que permite ser modelada progressivamente.
 * 
 * @author lundberg
 * 
 */
public class DeclarableRewardModel implements RewardModel {

	private final Map<State, Double> rewards = new HashMap<State, Double>();

	private final Map<Action, Double> costs = new HashMap<Action, Double>();

	private Double discountFactor;

	private Double defaultReward;

	private Double defaultCost;

	@Override
	public double reward(State state) {
		if (rewards.containsKey(state)) {
			return rewards.get(state);
		} else if (defaultReward != null) {
			return defaultReward;
		} else {
			throw new IllegalArgumentException("Não é possível definir recompensa do estado " + state);
		}
	}

	@Override
	public double cost(Action action) {
		if (costs.containsKey(action)) {
			return costs.get(action);
		} else if (defaultCost != null) {
			return defaultCost;
		} else {
			throw new IllegalArgumentException("Não é possível definir custo da ação " + action);
		}
	}

	@Override
	public double discountFactor() {
		if (discountFactor == null) {
			throw new IllegalStateException("Fator de desconto não definido.");
		} else {
			return discountFactor;
		}
	}

	public void setDiscountFactor(Double discountFactor) {
		this.discountFactor = discountFactor;
	}

	public void setDefaultReward(Double defaultReward) {
		this.defaultReward = defaultReward;
	}

	public void setDefaultCost(Double defaultCost) {
		this.defaultCost = defaultCost;
	}

	public void setReward(State state, double reward) {
		this.rewards.put(state, reward);
	}

	public void setCost(Action action, double cost) {
		this.costs.put(action, cost);
	}
}
